package com.example.zuoye2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {
    private EditText uname;
    private EditText upwd;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        uname = findViewById(R.id.name);
        upwd = findViewById(R.id.pwd);
        btn_login = findViewById(R.id.login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = uname.getText().toString();
                String userpwd = upwd.getText().toString();
                new Thread() {
                    public void run() {
                        String r = login(username, userpwd);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject object = new JSONObject(r);
                                    int error = object.optInt("error");
                                    if (error == 0) {
                                        Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                                        Uri uri = Uri.parse("https://www.wanandroid.com");
                                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                        startActivity(intent);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                }.start();

            }
        });
    }

    private String login(String name, String pwd) {
        try {
            URL url = new URL("https://www/wanandroid.com/user/login");
            HttpURLConnection cn = (HttpURLConnection) url.openConnection();
            cn.setRequestMethod("POST");
            cn.setDoOutput(true);
            cn.setDoInput(true);
            cn.setUseCaches(false);
            String data = "username=" + URLEncoder.encode(name, "utf-8") + "&password=" + URLEncoder.encode(pwd, "utf-8");
            OutputStream outputStream = cn.getOutputStream();
            outputStream.write(data.getBytes());
            outputStream.flush();
            if (cn.getResponseCode() == 200) {
                InputStream inputStream = cn.getInputStream();
                String str = Stream(inputStream);
                System.out.println(str);
                return str;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    private String Stream(InputStream is){
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
        StringBuffer stringBuffer = new StringBuffer();
        String Line;
        try{
            while((Line=bufferedReader.readLine())!= null){
                stringBuffer.append(Line);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }
}